package main;

import java.util.Date;
import java.util.Vector;

import javax.sound.sampled.AudioInputStream;

import jade.core.AID;
import jade.core.Agent;
import jade.core.behaviours.OneShotBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.core.behaviours.WakerBehaviour;
import jade.domain.FIPANames;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.UnreadableException;
import jade.proto.AchieveREInitiator;

public class AskingAgent extends Agent {

	@Override
	protected void setup() {
		super.setup();

		addBehaviour(new TickerBehaviour(this, 3000) {
			@Override
			protected void onTick() {
				ACLMessage msg = new ACLMessage(ACLMessage.REQUEST);
				msg.addReceiver(new AID("Mapa",AID.ISLOCALNAME));
				msg.setProtocol(FIPANames.InteractionProtocol.FIPA_REQUEST);
				msg.setReplyByDate(new Date(System.currentTimeMillis() + 10000));
				addBehaviour(new AskForLocation(myAgent, msg));
			}
		});
	}

	class AskForLocation extends AchieveREInitiator {

		public AskForLocation(Agent a, ACLMessage msg) {
			super(a, msg);
			System.out.println("AskForLocation");

		}

		@Override
		protected void handleInform(ACLMessage inform) {
			try {
				System.out.println(inform.getContentObject().toString());
			} catch (UnreadableException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		@Override
		protected void handleRefuse(ACLMessage refuse) {
			System.out.println("Agent " + refuse.getSender().getName()
					+ " refused to perform the requested action");
		}

		@Override
		protected void handleFailure(ACLMessage failure) {
			if (failure.getSender().equals(myAgent.getAMS())) {
				// FAILURE notification from the JADE runtime: the receiver
				// does not exist
				System.out.println("Responder does not exist");
			} else {
				System.out.println("Agent " + failure.getSender().getName()
						+ " failed to perform the requested action");
			}
		}

		@Override
		protected void handleAllResultNotifications(Vector notifications) {

		}
	}
}
